﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:demandplaninfo
	/// </summary>
	public partial class demandplaninfo
	{
		public demandplaninfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("dpid", "demandplaninfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int dpid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from demandplaninfo");
			strSql.Append(" where dpid=@dpid");
			SqlParameter[] parameters = {
					new SqlParameter("@dpid", SqlDbType.Int,4)
			};
			parameters[0].Value = dpid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        public int Addbasic(lcs.Model.demandplaninfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into demandplaninfo(");
            strSql.Append("pbh,uid,ptype,inputtime,proid,mtid,sid)");
            strSql.Append(" values (");
            strSql.Append("@pbh,@uid,@ptype,@inputtime,@proid,@mtid,@sid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@pbh", SqlDbType.NVarChar,20),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@ptype", SqlDbType.Int,4),					
					new SqlParameter("@inputtime", SqlDbType.SmallDateTime),
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
                                        new SqlParameter("@mtid", SqlDbType.Int,4),
                                        new SqlParameter("@sid", SqlDbType.Int,4)};
            parameters[0].Value = model.pbh;
            parameters[1].Value = model.uid;
            parameters[2].Value = model.ptype;
            parameters[3].Value = DateTime.Now;
            parameters[4].Value = model.proid;
            parameters[5].Value = model.mtid;
            parameters[6].Value = model.sid; 
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.demandplaninfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into demandplaninfo(");
			strSql.Append("pbh,uid,ptype,npmethod,fundsource,inputtime,invoicetype,recomsup,skillfile,status,ordernum,paymethod,signtime,batchnum,bagnum,scorefile,tendertime)");
			strSql.Append(" values (");
			strSql.Append("@pbh,@uid,@ptype,@npmethod,@fundsource,@inputtime,@invoicetype,@recomsup,@skillfile,@status,@ordernum,@paymethod,@signtime,@batchnum,@bagnum,@scorefile,@tendertime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@pbh", SqlDbType.NVarChar,20),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@ptype", SqlDbType.Int,4),
					new SqlParameter("@npmethod", SqlDbType.NVarChar,10),
					new SqlParameter("@fundsource", SqlDbType.NVarChar,20),
					new SqlParameter("@inputtime", SqlDbType.SmallDateTime),
					new SqlParameter("@invoicetype", SqlDbType.NVarChar,10),
					new SqlParameter("@recomsup", SqlDbType.Int,4),
					new SqlParameter("@skillfile", SqlDbType.NVarChar,1500),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@ordernum", SqlDbType.NVarChar,20),
					new SqlParameter("@paymethod", SqlDbType.NVarChar,10),
					new SqlParameter("@signtime", SqlDbType.SmallDateTime),
					new SqlParameter("@batchnum", SqlDbType.NVarChar,20),
					new SqlParameter("@bagnum", SqlDbType.NVarChar,20),
					new SqlParameter("@scorefile", SqlDbType.NVarChar,150),
					new SqlParameter("@tendertime", SqlDbType.SmallDateTime)};
			parameters[0].Value = model.pbh;
			parameters[1].Value = model.uid;
			parameters[2].Value = model.ptype;
			parameters[3].Value = model.npmethod;
			parameters[4].Value = model.fundsource;
			parameters[5].Value = model.inputtime;
			parameters[6].Value = model.invoicetype;
			parameters[7].Value = model.recomsup;
			parameters[8].Value = model.skillfile;
			parameters[9].Value = model.status;
			parameters[10].Value = model.ordernum;
			parameters[11].Value = model.paymethod;
			parameters[12].Value = model.signtime;
			parameters[13].Value = model.batchnum;
			parameters[14].Value = model.bagnum;
			parameters[15].Value = model.scorefile;
			parameters[16].Value = model.tendertime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        /// <summary>
        /// 设置技术文档
        /// </summary>
        public bool SetFile(string filepath, int dpid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update demandplaninfo set ");
            strSql.Append("skillfile=@skillfile ");
            strSql.Append(" where dpid=@dpid");
            SqlParameter[] parameters = {					
                    new SqlParameter("@skillfile", SqlDbType.NVarChar,1500),
					new SqlParameter("@dpid", SqlDbType.Int,4)};
            parameters[0].Value = filepath;
            parameters[1].Value = dpid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 设置应用类推荐厂家
        /// </summary>
        public bool SetRecomsup(int rec,int dpid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update demandplaninfo set ");            
            strSql.Append("recomsup=@recomsup ");
            strSql.Append(" where dpid=@dpid");
            SqlParameter[] parameters = {					
                    new SqlParameter("@recomsup", SqlDbType.Int,4),
					new SqlParameter("@dpid", SqlDbType.Int,4)};
            parameters[0].Value =rec;
            parameters[1].Value =dpid; 
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.demandplaninfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update demandplaninfo set ");
			strSql.Append("pbh=@pbh,");
			strSql.Append("uid=@uid,");
			strSql.Append("ptype=@ptype,");
            strSql.Append("proid=@proid, ");
            //strSql.Append("npmethod=@npmethod,");
            //strSql.Append("fundsource=@fundsource,");
            //strSql.Append("inputtime=@inputtime,");
            strSql.Append("invoicetype=@invoicetype ");
            //strSql.Append("recomsup=@recomsup,");
            //strSql.Append("skillfile=@skillfile,");
            //strSql.Append("status=@status,");
            //strSql.Append("ordernum=@ordernum,");
            //strSql.Append("paymethod=@paymethod,");
            //strSql.Append("signtime=@signtime,");
            //strSql.Append("batchnum=@batchnum,");
            //strSql.Append("bagnum=@bagnum,");
            //strSql.Append("scorefile=@scorefile,");
            //strSql.Append("tendertime=@tendertime");
			strSql.Append(" where dpid=@dpid");
			SqlParameter[] parameters = {
					new SqlParameter("@pbh", SqlDbType.NVarChar,20),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@ptype", SqlDbType.Int,4),
                    new SqlParameter("@proid", SqlDbType.NVarChar,50),
                    //new SqlParameter("@npmethod", SqlDbType.NVarChar,10),
                    //new SqlParameter("@fundsource", SqlDbType.NVarChar,20),
                    //new SqlParameter("@inputtime", SqlDbType.SmallDateTime),
                    new SqlParameter("@invoicetype", SqlDbType.NVarChar,10),
                    //new SqlParameter("@recomsup", SqlDbType.Int,4),
                    //new SqlParameter("@skillfile", SqlDbType.NVarChar,1500),
                    //new SqlParameter("@status", SqlDbType.Int,4),
                    //new SqlParameter("@ordernum", SqlDbType.NVarChar,20),
                    //new SqlParameter("@paymethod", SqlDbType.NVarChar,10),
                    //new SqlParameter("@signtime", SqlDbType.SmallDateTime),
                    //new SqlParameter("@batchnum", SqlDbType.NVarChar,20),
                    //new SqlParameter("@bagnum", SqlDbType.NVarChar,20),
                    //new SqlParameter("@scorefile", SqlDbType.NVarChar,150),
                    //new SqlParameter("@tendertime", SqlDbType.SmallDateTime),
					new SqlParameter("@dpid", SqlDbType.Int,4)};
			parameters[0].Value = model.pbh;
			parameters[1].Value = model.uid;
			parameters[2].Value = model.ptype;
			parameters[3].Value = model.proid;
            parameters[4].Value = model.invoicetype;
			parameters[5].Value = model.dpid;
            //parameters[5].Value = model.inputtime;
            //parameters[6].Value = model.invoicetype;
            //parameters[7].Value = model.recomsup;
            //parameters[8].Value = model.skillfile;
            //parameters[9].Value = model.status;
            //parameters[10].Value = model.ordernum;
            //parameters[11].Value = model.paymethod;
            //parameters[12].Value = model.signtime;
            //parameters[13].Value = model.batchnum;
            //parameters[14].Value = model.bagnum;
            //parameters[15].Value = model.scorefile;
            //parameters[16].Value = model.tendertime;
            //parameters[17].Value = model.dpid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int dpid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from demandplaninfo ");
			strSql.Append(" where dpid=@dpid");
			SqlParameter[] parameters = {
					new SqlParameter("@dpid", SqlDbType.Int,4)
			};
			parameters[0].Value = dpid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string dpidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from demandplaninfo ");
			strSql.Append(" where dpid in ("+dpidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.demandplaninfo GetModel(int dpid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  * from demandplaninfo ");
			strSql.Append(" where dpid=@dpid");
			SqlParameter[] parameters = {
					new SqlParameter("@dpid", SqlDbType.Int,4)
			};
			parameters[0].Value = dpid;

			lcs.Model.demandplaninfo model=new lcs.Model.demandplaninfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.demandplaninfo DataRowToModel(DataRow row)
		{
			lcs.Model.demandplaninfo model=new lcs.Model.demandplaninfo();
			if (row != null)
			{
				if(row["dpid"]!=null && row["dpid"].ToString()!="")
				{
					model.dpid=int.Parse(row["dpid"].ToString());
				}
				if(row["pbh"]!=null)
				{
					model.pbh=row["pbh"].ToString();
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["ptype"]!=null)
				{
					model.ptype=int.Parse(row["ptype"].ToString());
				}
				if(row["npmethod"]!=null)
				{
					model.npmethod=row["npmethod"].ToString();
				}
				if(row["fundsource"]!=null)
				{
					model.fundsource=row["fundsource"].ToString();
				}
				if(row["inputtime"]!=null && row["inputtime"].ToString()!="")
				{
					model.inputtime=DateTime.Parse(row["inputtime"].ToString());
				}
				if(row["invoicetype"]!=null)
				{
					model.invoicetype=row["invoicetype"].ToString();
				}
				if(row["recomsup"]!=null && row["recomsup"].ToString()!="")
				{
					model.recomsup=int.Parse(row["recomsup"].ToString());
				}
				if(row["skillfile"]!=null)
				{
					model.skillfile=row["skillfile"].ToString();
				}
				if(row["status"]!=null && row["status"].ToString()!="")
				{
					model.status=int.Parse(row["status"].ToString());
				}
				if(row["ordernum"]!=null)
				{
					model.ordernum=row["ordernum"].ToString();
				}
				if(row["paymethod"]!=null)
				{
					model.paymethod=row["paymethod"].ToString();
				}
				if(row["signtime"]!=null && row["signtime"].ToString()!="")
				{
					model.signtime=DateTime.Parse(row["signtime"].ToString());
				}
				if(row["batchnum"]!=null)
				{
					model.batchnum=row["batchnum"].ToString();
				}
				if(row["bagnum"]!=null)
				{
					model.bagnum=row["bagnum"].ToString();
				}
                if (row["bagname"] != null)
                {
                    model.bagname = row["bagname"].ToString();
                }
				if(row["scorefile"]!=null)
				{
					model.scorefile=row["scorefile"].ToString();
				}
				if(row["tendertime"]!=null && row["tendertime"].ToString()!="")
				{
					model.tendertime=DateTime.Parse(row["tendertime"].ToString());
				}
                if (row["proid"] != null && row["proid"].ToString() != "")
                {
                    model.proid = row["proid"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select * ");
			strSql.Append(" FROM Vdemandplaninfo where 1=1");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" dpid,pbh,uid,ptype,npmethod,fundsource,inputtime,invoicetype,recomsup,skillfile,status,ordernum,paymethod,signtime,batchnum,bagnum,scorefile,tendertime ");
			strSql.Append(" FROM demandplaninfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM demandplaninfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.dpid desc");
			}
			strSql.Append(")AS Row, T.*  from demandplaninfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "demandplaninfo";
			parameters[1].Value = "dpid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateModelBidSubmit(lcs.Model.demandplaninfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update demandplaninfo set ");
           
            strSql.Append("npmethod=@npmethod,");
            strSql.Append("batchnum=@batchnum,");
            strSql.Append("bagnum=@bagnum, ");
            strSql.Append("bagname=@bagname, ");
            strSql.Append("ordernum=@ordernum ");
            strSql.Append(" where dpid=@dpid");
            SqlParameter[] parameters = {			
                    new SqlParameter("@npmethod", SqlDbType.NVarChar,50),
                    new SqlParameter("@batchnum", SqlDbType.NVarChar,20),
                    new SqlParameter("@bagnum", SqlDbType.NVarChar,20),
                    new SqlParameter("@bagname", SqlDbType.VarChar,200),
                     new SqlParameter("@ordernum", SqlDbType.VarChar,20),
					new SqlParameter("@dpid", SqlDbType.Int,4)};
            parameters[0].Value = model.npmethod;
            parameters[1].Value = model.batchnum;
            parameters[2].Value = model.bagnum;
            parameters[3].Value = model.bagname;
            parameters[4].Value = model.ordernum;  
            parameters[5].Value = model.dpid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		#endregion  ExtensionMethod
	}
}

